package cn.itcast.bos.web.action.common;


import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import org.springframework.data.domain.Page;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;


public abstract class BaseAction<T> extends ActionSupport implements ModelDriven<T>{
	
	//提供模型驱动：
	protected T model;
	@Override
	public T getModel(){
		return model;
		
	}
	
	//构造器完成，完成Model实例化：
	public BaseAction(){
		//构造子类对象获取，获取继承父类的泛型：
		Type genericSuperclass = this.getClass().getGenericSuperclass();
		//获取第一个参数的泛型：
		//获取直接超类的泛型；
		ParameterizedType parameterizedType = (ParameterizedType)genericSuperclass;
		System.out.println("=========================");
		System.out.println(parameterizedType);//cn.itcast.bos.web.action.common.BaseAction<cn.itcast.bos.domain.base.Standard>
		
		Class<T> modleClass = (Class<T>)parameterizedType.getActualTypeArguments()[0];
		try {
			model = modleClass.newInstance();
		} catch (InstantiationException | IllegalAccessException e) {
			
			e.printStackTrace();
			System.out.println("模型创建失败。。。。");
		} 
	}
	
	
	// 接收分页查询参数
		protected int page;
		protected int rows;

		public void setPage(int page) {
			this.page = page;
		}

		public void setRows(int rows) {
			this.rows = rows;
		}

		// 将分页查询结果数据，压入值栈的方法
		protected void pushPageDataToValueStack(Page<T> pageData) {
			Map<String, Object> result = new HashMap<String, Object>();
			result.put("total", pageData.getTotalElements());
			result.put("rows", pageData.getContent());

			ActionContext.getContext().getValueStack().push(result);
		}
}
